package CCRMClient.GUI;

import javax.swing.JPanel;
import javax.swing.border.LineBorder;

import java.awt.Color;
import java.awt.Dimension;

import javax.swing.JLabel;

import java.awt.Font;

import javax.swing.JOptionPane;
import javax.swing.SwingConstants;
import javax.swing.JTextField;

import java.awt.SystemColor;
import java.sql.ResultSet;
import java.sql.SQLException;

import javax.swing.JButton;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.JTableHeader;
import javax.swing.table.TableColumn;
import javax.swing.table.TableColumnModel;
import javax.swing.JScrollPane;

import CCRMClient.ClientStart;
import CCRMClient.Controllers.CatalogManager;
import Common.SERVER_COMMANDS;
import net.proteanit.sql.DbUtils;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.io.IOException;


/**
 * This class implements the add type to product category screen
 * @author Ori Ziv
 *
 */
public class AddTypeScreen extends JPanel {
	private JLabel lblMangeCategoryTypes;
	private JLabel lblCategoryName;
	private JTextField textCategoryName;
	private JButton btnSearch;
	private JButton btnAddNewType;
	private JTextField textTypeName;
	private JLabel lblConcurrentTypes;
	private JButton btnGoBack;
	private JScrollPane scpTypes;
	private JTable tblTypes;
	private String currentCategory;
	private JButton btnRefresh;
	

	/**
	 * Create the panel.
	 */
	public AddTypeScreen() {

		initialize();
	}
	
	public AddTypeScreen(ResultSet categoryTypes, String catName) {
		this();
		this.currentCategory = catName;
		loadRStoTable(categoryTypes);
		tblTypes.setEnabled(false);
		textCategoryName.setText(catName);
		textCategoryName.setEditable(false);
		btnSearch.setEnabled(false);
		textTypeName.setEditable(true);
		btnAddNewType.setEnabled(true);
		btnRefresh.setEnabled(true);
	}
	
	private void loadRStoTable(ResultSet categoryTypes) {
		tblTypes.setModel(DbUtils.resultSetToTableModel(categoryTypes));
		JTableHeader th = tblTypes.getTableHeader();
		TableColumnModel tcm = th.getColumnModel();
		TableColumn tc = tcm.getColumn(0);
		tc.setHeaderValue("Types related to " + currentCategory + ":");
		th.repaint();
	}

	private void initialize() {
		setSize (600, 430);
		setBackground(Color.WHITE);
		setBorder(new LineBorder(new Color(0, 0, 0)));
		setLayout(null);
		add(getLblMangeCategoryTypes());
		add(getLblCategoryName());
		add(getTextCategoryName());
		add(getBtnSearch());
		add(getBtnAddNewType());
		add(getTextTypeName());
		add(getLblConcurrentTypes());
		add(getBtnGoBack());
		add(getScpTypes());
		add(getBtnRefresh());
	}
	JLabel getLblMangeCategoryTypes() {
		if (lblMangeCategoryTypes == null) {
			lblMangeCategoryTypes = new JLabel("Manage Category Types");
			lblMangeCategoryTypes.setHorizontalAlignment(SwingConstants.CENTER);
			lblMangeCategoryTypes.setForeground(Color.RED);
			lblMangeCategoryTypes.setFont(new Font("David", Font.BOLD, 26));
			lblMangeCategoryTypes.setBackground(Color.WHITE);
			lblMangeCategoryTypes.setBounds(0, 0, 573, 49);
		}
		return lblMangeCategoryTypes;
	}
	JLabel getLblCategoryName() {
		if (lblCategoryName == null) {
			lblCategoryName = new JLabel("Category Name:");
			lblCategoryName.setFont(new Font("David", Font.BOLD, 20));
			lblCategoryName.setBounds(10, 45, 164, 29);
		}
		return lblCategoryName;
	}
	JTextField getTextCategoryName() {
		if (textCategoryName == null) {
			textCategoryName = new JTextField();
			textCategoryName.setText("fill in category name");
			textCategoryName.setColumns(10);
			textCategoryName.setBackground(SystemColor.menu);
			textCategoryName.setBounds(173, 50, 251, 20);
		}
		return textCategoryName;
	}
	JButton getBtnSearch() {
		if (btnSearch == null) {
			btnSearch = new JButton("Search");
			btnSearch.addActionListener(new BtnSearchActionListener());
			btnSearch.setFont(new Font("Tahoma", Font.BOLD, 12));
			btnSearch.setBounds(439, 49, 124, 23);
		}
		return btnSearch;
	}
	JButton getBtnAddNewType() {
		if (btnAddNewType == null) {
			btnAddNewType = new JButton("Add New Type");
			btnAddNewType.addActionListener(new BtnAddNewTypeActionListener());
			btnAddNewType.setEnabled(false);
			btnAddNewType.setFont(new Font("Tahoma", Font.BOLD, 12));
			btnAddNewType.setBounds(20, 85, 124, 23);
		}
		return btnAddNewType;
	}
	JTextField getTextTypeName() {
		if (textTypeName == null) {
			textTypeName = new JTextField();
			textTypeName.setEditable(false);
			textTypeName.setText("fill in new type name");
			textTypeName.setColumns(10);
			textTypeName.setBackground(SystemColor.menu);
			textTypeName.setBounds(173, 87, 251, 20);
		}
		return textTypeName;
	}
	JLabel getLblConcurrentTypes() {
		if (lblConcurrentTypes == null) {
			lblConcurrentTypes = new JLabel("Concurrent types:");
			lblConcurrentTypes.setHorizontalAlignment(SwingConstants.LEFT);
			lblConcurrentTypes.setForeground(Color.RED);
			lblConcurrentTypes.setFont(new Font("David", Font.BOLD, 20));
			lblConcurrentTypes.setBackground(Color.WHITE);
			lblConcurrentTypes.setBounds(10, 118, 172, 38);
		}
		return lblConcurrentTypes;
	}
	JButton getBtnGoBack() {
		if (btnGoBack == null) {
			btnGoBack = new JButton("Go Back");
			btnGoBack.addActionListener(new BtnGoBackActionListener());
			btnGoBack.setFont(new Font("Tahoma", Font.BOLD, 12));
			btnGoBack.setBounds(10, 353, 124, 23);
		}
		return btnGoBack;
	}
	/**
	 * This inner class implements event listener for pressing Go Back button
	 * @author Ori Ziv
	 *
	 */
	private class BtnGoBackActionListener implements ActionListener {
		public void actionPerformed(ActionEvent arg0) {
			ClientStart.panelSwicher(new CatalogScreen());
		}
	}
	/**
	 * This inner class implements event listener for pressing Search button
	 * @author Ori Ziv
	 *
	 */
	private class BtnSearchActionListener implements ActionListener {
		public void actionPerformed(ActionEvent e) {
			try {
				String catName = textCategoryName.getText();
				try {
					ClientStart.client.handleMessageFromGUI(SERVER_COMMANDS.GET_CATEGORY_TYPES, CatalogManager.searchCategory(catName));
				} catch (IOException e1) {
					JOptionPane.showMessageDialog(null, "Lost connection with server", "Communication Error", JOptionPane.ERROR_MESSAGE);
					ClientStart.quitApp();
				}
			} catch (NullPointerException ex) {
				JOptionPane.showMessageDialog(null, "You must supply category name", "Search category problem", JOptionPane.INFORMATION_MESSAGE);
			}
		}
	}
	/**
	 * This inner class implements event listener for pressing Add New Type button
	 * @author Ori Ziv
	 *
	 */
	private class BtnAddNewTypeActionListener implements ActionListener {
		public void actionPerformed(ActionEvent e) {
			try {
				String newType = textTypeName.getText();
				try {
					ClientStart.client.handleMessageFromGUI(SERVER_COMMANDS.ADD_TYPE, CatalogManager.addType(newType, textCategoryName.getText()));
				} catch (IOException e1) {
					JOptionPane.showMessageDialog(null, "Lost connection with server", "Communication Error", JOptionPane.ERROR_MESSAGE);
					ClientStart.quitApp();
				}
			} catch (NullPointerException ex) {
				JOptionPane.showMessageDialog(null, "You must supply type name", "Add type Problem", JOptionPane.INFORMATION_MESSAGE);
			}
		}
	}
	/**
	 * This inner class implements event listener for pressing Refresh button
	 * @author Ori Ziv
	 *
	 */
	private class BtnRefreshActionListener implements ActionListener {
		public void actionPerformed(ActionEvent arg0) {
			try {
				String catName = textCategoryName.getText();
				try {
					ClientStart.client.handleMessageFromGUI(SERVER_COMMANDS.GET_CATEGORY_TYPES, CatalogManager.searchCategory(catName));
				} catch (IOException e1) {
					JOptionPane.showMessageDialog(null, "Lost connection with server", "Communication Error", JOptionPane.ERROR_MESSAGE);
					ClientStart.quitApp();
				}
			} catch (NullPointerException ex) {
				JOptionPane.showMessageDialog(null, "You must supply category name", "Search category problem", JOptionPane.INFORMATION_MESSAGE);
			}
		}
	}
	JScrollPane getScpTypes() {
		if (scpTypes == null) {
			scpTypes = new JScrollPane();
			scpTypes.setBounds(192, 131, 232, 224);
			scpTypes.setViewportView(getTblTypes());
		}
		return scpTypes;
	}
	JTable getTblTypes() {
		if (tblTypes == null) {
			tblTypes = new JTable();
			tblTypes.setFont(new Font("David", Font.BOLD, 11));
			tblTypes.setModel(new DefaultTableModel(
				new Object[][] {
					{null},
				},
				new String[] {
					"Types:"
				}
			) {
				Class[] columnTypes = new Class[] {
					String.class
				};
				public Class getColumnClass(int columnIndex) {
					return columnTypes[columnIndex];
				}
				boolean[] columnEditables = new boolean[] {
					false
				};
				public boolean isCellEditable(int row, int column) {
					return columnEditables[column];
				}
			});
		}
		return tblTypes;
	}
	JButton getBtnRefresh() {
		if (btnRefresh == null) {
			btnRefresh = new JButton("Refresh");
			btnRefresh.addActionListener(new BtnRefreshActionListener());
			btnRefresh.setEnabled(false);
			btnRefresh.setFont(new Font("Tahoma", Font.BOLD, 12));
			btnRefresh.setBounds(439, 86, 124, 23);
		}
		return btnRefresh;
	}
}
